package com.jian8.juc.CompletableFuture;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @author wu
 * @create 2023-01-30-16:43
 */
public class CompletableFutureNetMallDemo {
    static List<NetMall> list = Arrays.asList(
            new NetMall("jd"),
            new NetMall("pdd"),
            new NetMall("taobao"),
            new NetMall("dangdangwang"),
            new NetMall("tmall"));

    //同步 ,step by step

    /**
     * List<NetMall>  ---->   List<String>
     *
     * @param list
     * @param productName
     * @return
     */
    public static List<String> getPriceByStep(List<NetMall> list, String productName) {

        return list
                .stream()
                .map(netMall -> String.format(productName + " in %s price is %.2f", netMall.getMallName(),
                        netMall.calcPrice(productName)))
                .collect(Collectors.toList());
    }
    //异步 ,多箭齐发

    /**
     * List<NetMall>  ---->List<CompletableFuture<String>> --->   List<String>
     *
     * @param list
     * @param productName
     * @return
     */
    public static List<String> getPriceByASync(List<NetMall> list, String productName) {
        return list
                .stream()
                .map(netMall ->
                        CompletableFuture.supplyAsync(() ->
                                String.format(productName + " is %s price is %.2f", netMall.getMallName(), netMall.calcPrice(productName)))
                )
                .collect(Collectors.toList())
                .stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();
        List<String> list1 = getPriceByStep(list, "mysql");
        for (String element : list1) {
            System.out.println(element);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("----costTime: " + (endTime - startTime) + " 毫秒");

        System.out.println();

        long startTime2 = System.currentTimeMillis();
        List<String> list2 = getPriceByASync(list, "mysql");
        for (String element : list2) {
            System.out.println(element);
        }
        long endTime2 = System.currentTimeMillis();
        System.out.println("----costTime: " + (endTime2 - startTime2) + " 毫秒");

    }

    /**
     * /**
     * 	 * 聚合三个接口数据
     * 	 * @param memberInfoVo
     * 	 * @return
     *
     *    @Override
     *

    public CustomerListGradeAndEquityRespVo getCustomerListGradeAndEquity(CombineMemberInfoVo memberInfoVo) {
     *Long startTime= S ystem.currentTimeMillis();
     *CustomerListGradeAndEquityRespVo respVo = new CustomerListGradeAndEquityRespVo();
     *        // (1). 查询会员等级等基本信息
     *CompletableFuture<CustomerGradeRespVo> GradeCompletableFuture  = CompletableFuture.supplyAsync(() -> {
     *CustomerGradeRespVo customerGrade = combineScrmApi.getCustomerGrade(memberInfoVo.getCustomerId());
     *respVo.setCustomer(customerGrade);
     *return customerGrade;
     *},executor);
     *        // (2). 根据会员、等级查询会员可享受权益列表(需要依赖1结果)
     *CompletableFuture<Void> EquitiesFuture =G radeCompletableFuture.thenAcceptAsync((res)- >{
     *listEquitiesVo equitiesVo = new listEquitiesVo();
     *equitiesVo.setCustomerId(memberInfoVo.getCustomerId());
     *equitiesVo.setMerchantId(res.getMerchantId());
     *equitiesVo.setGradeCode(res.getGradeCode());
     *List<EquitiesRespVo> listEquities = combineScrmApi.listEquities(equitiesVo);
     *respVo.setEquitiesList(listEquities);
     *},executor);
     *        // (3). 查询等级列表
     *CompletableFuture<Void> gradeConfigFuture= C ompletableFuture.runAsync(()- >{
     *List<GradeConfigRespVo> gradeConfig = combineScrmApi.listGradeConfigByMerchantId(memberInfoVo.getMerchantId());
     *respVo.setGradeConfigList(gradeConfig);
     *},executor);
     *        // 等待所有任务都完成再返回
     *CompletableFuture.allOf(GradeCompletableFuture,E quitiesFuture,g radeConfigFuture).join();
     *log.info("combine组合接口三个(getCustomerListGradeAndEquity) ==> 异步消耗时间:{}",(System.currentTimeMillis()- s tartTime));
     *log.info("combine组合接口三个(getCustomerListGradeAndEquity) ==> 返回数据为:{}",r espVo);
     *return respVo;
     *    }
     * ————————————————
     *版权声明：本文为CSDN博主「所得皆惊喜」的原创文章，遵循CC 4.0BY-SA版权协议，转载请附上原文出处链接及本声明。
     *原文链接：https://blog.csdn.net/TZ845195485/article/details/114692426
     */
}